home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / interval.lha / interval / interval.h < prev    next >
C/C++ Source or Header  |  1989-10-31  |  2KB  |  43 lines

  1. // Class interval
  2. //    A definition of intervals for interval arithmetic.
  3. //
  4. //    The representation of the interval is two doubles
  5. //    but conversion operators are defined for int and float.
  6. //
  7. //    Error handling is weak.  In an IEEE compliant implementation,
  8. //    quiet NANs could be used to indicate bounds that were
  9. //    invalid.  Otherwise, more state (and checking) would
  10. //    need to be added.
  11.  
  12. // To get the definition of MAXDOUBLE
  13. #include <values.h>
  14.  
  15. class interval {
  16.     double lo_bound, hi_bound;
  17. public:
  18.     double lo(void)        { return lo_bound; }
  19.     double hi(void)        { return hi_bound; }
  20.     double width(void)        { return hi_bound - lo_bound; }
  21.     interval()            { lo_bound = -MAXDOUBLE; hi_bound = MAXDOUBLE; }
  22.     interval(double l, double h){ lo_bound = l<h?l:h; hi_bound = l<h?h:l; }
  23.     interval(double f)        { lo_bound = f; hi_bound = f; }
  24.     interval(float l, float h)    { lo_bound = l<h?l:h; hi_bound = l<h?h:l; }
  25.     interval(float f)        { lo_bound = f; hi_bound = f; }
  26.     interval(int l, int h)    { lo_bound = l<h?l:h; hi_bound = l<h?h:l; }
  27.     interval(int i)        { lo_bound = i; hi_bound = i; }
  28.     interval operator+(interval I);
  29.     interval operator-(interval I);
  30.     interval operator*(interval I);
  31.     interval operator/(interval I);
  32.     interval &operator+=(interval I);
  33.     interval &operator-=(interval I);
  34.     interval &operator*=(interval I);
  35.     interval &operator/=(interval I);
  36.     int contains(interval I)    { return lo_bound <= I.lo_bound && 
  37.                      hi_bound >= I.hi_bound; }
  38.     int overlaps(interval I)    { return lo_bound <= I.hi_bound && 
  39.                      hi_bound >= I.lo_bound; }
  40.     int equal(interval I)    { return lo_bound == I.lo_bound && 
  41.                      hi_bound == I.hi_bound; }
  42. };
  43.